package com.red.wood.module.eval.dao.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.red.wood.common.domain.Page;
import com.red.wood.common.springjdbc.SqlServerDao;
import com.red.wood.common.util.CommonUtils;
import com.red.wood.common.util.IMap;
import com.red.wood.module.eval.dao.HouseNodeLinkDao;
import com.red.wood.module.eval.mybatis.service.EvalPassEnterpriseService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Repository
public class HouseNodeLinkDaoImpl implements HouseNodeLinkDao{

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private SqlServerDao sqlServerDao;
	
	@Override
	public Map<String, Object> getPageData(IMap param) {
		String count = "0";
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		list = listPageData(param);
		count = listCount(param);
		
		Map<String, Object> rs = new HashMap<String, Object>();
		rs.put("totalCount", count);
		rs.put("pageData", list);
		return rs;
	}

	public List<Map<String, Object>> listPageData(IMap param){
		String singleTaskId = CommonUtils.valueOf(param.get("singleTaskId"));
		String apply_business = CommonUtils.valueOf(param.get("apply_business"));
		Page page = (Page) param.get("page");
		int offset = (page.getPageNo() - 1) * page.getPageSize();
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(apply_business, "ISP") || StringUtils.equals(apply_business, "IDC(不含互联网资源协作)")) {
			sql.append("select gameProvince as province,gameCity as city,houseName as name,houseAddress as address from t_eval_application_cdn");
			sql.append(" where singleTaskId = '"+singleTaskId+"'");
			sql.append(queryConf(param,1));
		}else {
			sql.append("select province,city,name,address from t_eval_applicationcdn_node");
			sql.append(" where singleTaskId = '"+singleTaskId+"'");
			sql.append(queryConf(param,2));
		}
		sql.append(" limit " + page.getPageSize() + " offset " + offset+ " ;");
		return jdbcTemplate.queryForList(sql.toString());
	}
	
	public String listCount(IMap param) {
		String singleTaskId = CommonUtils.valueOf(param.get("singleTaskId"));
		String apply_business = CommonUtils.valueOf(param.get("apply_business"));
		StringBuffer sql = new StringBuffer();
		if(StringUtils.equals(apply_business, "ISP") || StringUtils.equals(apply_business, "IDC(不含互联网资源协作)")) {
			sql.append("select count(1) count from t_eval_application_cdn");
			sql.append(" where singleTaskId = '"+singleTaskId+"'");
			sql.append(queryConf(param,1));
		}else {
			sql.append("select count(1) count from t_eval_applicationcdn_node");
			sql.append(" where singleTaskId = '"+singleTaskId+"'");
			sql.append(queryConf(param,2));
		}
		Map<String, Object> map = jdbcTemplate.queryForList(sql.toString()).get(0);
		String count = map.get("count").toString();
		return count;
	}
	
	public StringBuffer queryConf(IMap param,int type){
		StringBuffer sb = new StringBuffer();
		String singleTaskId = CommonUtils.valueOf(param.get("singleTaskId"));
		IMap map = sqlServerDao.getEnterpriseAuthority(param.getString("userId"));
		
		String input = "";
		try {
			input = IOUtils.toString(EvalPassEnterpriseService.class.getClassLoader().getResourceAsStream("user_parser.json"),"UTF-8");
		} catch (IOException e) {
			e.printStackTrace();
		}
		JSONArray jArray = JSONArray.fromObject(input);
		JSONObject jsonObject = jArray.getJSONObject(0);
		
		String loginUser = CommonUtils.valueOf(map.get("loginUser"));
		int userAuthority = Integer.parseInt(map.getString("userAuthority"));
		
		if(userAuthority == 4 || userAuthority == 5) {//增值企业,超级管理员
		}else {
			String value = jsonObject.getString(loginUser);
			if(type == 1) {
				if(userAuthority == 1) {//管局账号
					sb.append(" and gameProvince='"+value+"'");
				}else if(userAuthority == 2) {//集团账号(移动、电信、联通)
					sb.append(" and gameProvince in (select t.province from t_eval_application_coverinfo t  where t.singleTaskId = "+singleTaskId+" and t.coverName = '"+value+"')");
				}else if(userAuthority == 3) {//省级基础电信企业(分省和运营商),如北京电信
					String province = value.split("-")[0];
					String operator = value.split("-")[1];
					sb.append(" and gameProvince in (select t.province from t_eval_application_coverinfo t  where t.singleTaskId = "+singleTaskId+" and t.coverName = '"+operator+"' and t.province = '"+province+"')");
				}
			}else{
				if(userAuthority == 1) {//管局账号
					sb.append(" and province='"+value+"'");
				}else if(userAuthority == 2) {//集团账号(移动、电信、联通)
					sb.append(" and province in (select t.province from t_eval_application_coverinfo t  where t.singleTaskId = "+singleTaskId+" and t.coverName = '"+value+"')");
				}else if(userAuthority == 3) {//省级基础电信企业(分省和运营商),如北京电信
					String province = value.split("-")[0];
					String operator = value.split("-")[1];
					sb.append(" and province in (select t.province from t_eval_application_coverinfo t  where t.singleTaskId = "+singleTaskId+" and t.coverName = '"+operator+"' and t.province = '"+province+"')");
				}
			}
		}
		return sb;
	}
}
